//
//  MovieCollectionViewController.m
//  UI - 豆瓣
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "MovieCollectionViewController.h"
#import "MovieCollectionViewCell.h"
#import "AppTools.h"
#import "Movie.h"
#import "DateBaseSingleton.h"
#import "MovieDataViewController.h"

@interface MovieCollectionViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property(nonatomic, retain)Movie *movie;
@property(nonatomic, retain)NSMutableArray *bigArray;

@end

@implementation MovieCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"电影";
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.itemSize = CGSizeMake(110, 160);
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 10, 10, 10);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumLineSpacing = 20;
    
    
    UICollectionView *collectonView = [[UICollectionView alloc]initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
    [self.view addSubview:collectonView];
    collectonView.delegate = self;
    collectonView.dataSource = self;
    [collectonView registerClass:[MovieCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    collectonView.backgroundColor = [UIColor whiteColor];
    
    [flowLayout release];
    [collectonView release];
    self.bigArray = [NSMutableArray array];
    [AppTools getWithName:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" Block:^(id result) {
       
        NSMutableDictionary *dic = result;
        NSMutableArray *array = [dic objectForKey:@"result"];
        for (NSMutableDictionary *dic in array) {
            Movie *movie = [[Movie alloc] init];
            [movie setValuesForKeysWithDictionary:dic];
            [self.bigArray addObject:movie];
            [movie release];
        }
        [collectonView reloadData];
        
    }];
    
    self.navigationItem.rightBarButtonItem =
    [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_list@2x.png"] style:UIBarButtonItemStyleDone target:self action:@selector(rightAction:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    MovieDataViewController *movieDataVC = [[MovieDataViewController alloc] init];
    Movie *movie = [self.bigArray objectAtIndex:indexPath.row];
    movieDataVC.movieID = movie.movieId;
    [self.navigationController pushViewController:movieDataVC animated:YES];
    [movieDataVC release];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.bigArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *filePath = @"cell";
    MovieCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:filePath forIndexPath:indexPath];
    Movie *movie = [self.bigArray objectAtIndex:indexPath.item];
    cell.movie = movie;
//    cell.backgroundColor = [UIColor yellowColor];
    return cell;

    
}

-(void)rightAction: (UIBarButtonItem *)right{

    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
